package com.zhangyuji.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangyuji.common.MyCurrentId;
import com.zhangyuji.common.R;
import com.zhangyuji.entity.Orders;
import com.zhangyuji.service.AddressBookService;
import com.zhangyuji.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpServletRequest request){
        String userPhone = (String) request.getSession().getAttribute("user");
        MyCurrentId.setCurrentId(Long.valueOf(userPhone));
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<IPage> getUserPage(int page,int pageSize,HttpServletRequest request){
        String userPhone = (String) request.getSession().getAttribute("user");
        Long userId = addressBookService.getUserId(userPhone);
        IPage<Orders> iPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        orderService.page(iPage,wrapper);
        return R.success(iPage);
    }

    @GetMapping("/page")
    public R<IPage> getPage(int page,int pageSize){

        return null;
    }
}